package cn.yyd.org.service.impl;

import cn.yyd.basic.exception.BusinessException;
import cn.yyd.basic.utils.BaiduAuditUtils;
import cn.yyd.org.domain.Employee;
import cn.yyd.org.domain.Shop;
import cn.yyd.org.domain.ShopAuditLog;
import cn.yyd.org.mapper.EmployeeMapper;
import cn.yyd.org.mapper.ShopAuditLogMapper;
import cn.yyd.org.mapper.ShopMapper;
import cn.yyd.org.service.IShopService;
import cn.yyd.basic.service.impl.BaseServiceImpl;
import cn.yyd.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-07-01
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
    * @Title: settlement
    * @Description: 店铺入驻方法
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/7/1 14:10
    * @Parameters: [shop]
    * @Return void
    */
    @Override
    public void settlement(Shop shop) {
        if (StringUtils.isEmpty(shop.getName())){
            throw new RuntimeException("店铺名称不能为空，请重新输入！！！");
        }
        Shop loadShop = shopMapper.loadByName(shop.getName());
        if (loadShop != null ){
            throw new RuntimeException("店铺已经被注册,请重新输入");
        }

        /*Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if (!aBoolean){
                throw new RuntimeException("请输入合法信息！！！");
        }*/
        // 调用百度AI智能审核文本工具类，审核用户的店铺名称
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("请不要输入不合规范的店铺名称！！");
        }
        // 判断管理人员是否已经注册并且已经注册了店铺
        Employee admin = shop.getAdmin();
        Employee loadEmp = employeeMapper.loadByUserName(admin.getUsername());
        // 判断管理员和店铺是否存在
        if (loadEmp != null && loadEmp.getShopId() != null){
            throw new RuntimeException("该管理员已经绑定了店铺,请重新输入！");
        }
        // 判断管理员是否存在，不存在则添加保存起来
        if (loadEmp == null){
            employeeMapper.save(admin);
        }
        // 保存店铺信息
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);
        // 修改管理人员所属的店铺信息
        admin.setShopId(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    public Map<String, Object> loadShopEchartsData() {
       List<ShopVo> shopVos = shopMapper.loadShopEchartsData();
        List<Integer> x = shopVos.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> y = shopVos.stream().map(ShopVo::getCountNum).collect(Collectors.toList());
        HashMap<String, Object> data = new HashMap<>();
        data.put("x",x);
        data.put("y",y);
        return data;
    }

    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        // 修改店铺信息为未激活

        // 保存1店铺操作日志
        // 因为我们现在没有实现功能,所以暂时把操作人Id写死,填写一个平台员工Id
        shopAuditLog.setAuditId(334);
        // 设置操作日志记录中的店铺状态为未激活，自行实现，现在默认认为通过
        Shop shops = shopMapper.loadById(shopAuditLog.getShopId());
        shops.setState(1);
        shopMapper.update(shops);
        // 调用店铺日志保存方法，进行保存
        shopAuditLogMapper.save(shopAuditLog);
        // 发送激活邮件给用户，此处各位使用定义好的发送邮件工具类进行发送
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 设置发送人
        simpleMailMessage.setFrom("2864257810@qq.com");
        // 设置收件人，此处应根据店铺信息查询管理邮箱
        simpleMailMessage.setTo("2864257810@qq.com");
        // 设置邮件主题
        simpleMailMessage.setSubject("店铺审核结果通知");
        // 设置邮件文本内容
        simpleMailMessage.setText("您好，您的店铺已审核通过，请点击激活链接进行店铺激活。" +
                "激活后方可正常使用！" +
                "激活链接: http://localhost:8080/shop/audit/"+shopAuditLog.getShopId());
        // 使用邮件核心对象发送邮件
        javaMailSender.send(simpleMailMessage);
    }

    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        // 修改店铺信息为未激活
        // 保存1店铺操作日志
        // 因为我们现在没有实现功能,所以暂时把操作人Id写死,填写一个平台员工Id
        shopAuditLog.setAuditId(334);
        // 设置操作日志记录中的店铺状态为未激活，驳回
        Shop shops = shopMapper.loadById(shopAuditLog.getShopId());
        shops.setState(-1);
        shopMapper.update(shops);
        // 调用店铺日志保存方法，进行保存
        shopAuditLogMapper.save(shopAuditLog);
        // 发送激活邮件给用户，此处各位使用定义好的发送邮件工具类进行发送
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 设置发送人
        simpleMailMessage.setFrom("2864257810@qq.com");
        // 设置收件人，此处应根据店铺信息查询管理邮箱
        simpleMailMessage.setTo("2864257810@qq.com");
        // 设置邮件主题
        simpleMailMessage.setSubject("店铺审核结果通知");
        // 设置邮件文本内容
        simpleMailMessage.setText("您好，您的店铺审核未通过，请修改信息后重新提交！" +
                "失败原因！'" +shopAuditLog.getNote()+"'," +
                "激活链接: http://localhost:8081/shopRegisterAgain?shopId="+shopAuditLog.getShopId());
        // 使用邮件核心对象发送邮件
        javaMailSender.send(simpleMailMessage);
    }

    @Override
    public void dame(ShopAuditLog shopAuditLog) {
        // 修改店铺信息为未激活
        // 保存1店铺操作日志
        // 因为我们现在没有实现功能,所以暂时把操作人Id写死,填写一个平台员工Id
        shopAuditLog.setAuditId(334);
        // 设置操作日志记录中的店铺状态为未激活，驳回
        Shop shops = shopMapper.loadById(shopAuditLog.getShopId());
        shops.setState(3);
        shopMapper.update(shops);
        // 调用店铺日志保存方法，进行保存
        shopAuditLogMapper.save(shopAuditLog);
        // 发送激活邮件给用户，此处各位使用定义好的发送邮件工具类进行发送
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 设置发送人
        simpleMailMessage.setFrom("2864257810@qq.com");
        // 设置收件人，此处应根据店铺信息查询管理邮箱
        simpleMailMessage.setTo("2864257810@qq.com");
        // 设置邮件主题
        simpleMailMessage.setSubject("店铺审核结果通知");
        // 设置邮件文本内容
        simpleMailMessage.setText("您好，您的店铺审核未通过，拒绝！" +
                "拒绝！'" +shopAuditLog.getShopId());
        // 使用邮件核心对象发送邮件
        javaMailSender.send(simpleMailMessage);
    }
}
